#!/bin/bash

# try to connect to all robots via wlan.
# ATTENTION: Needs an aproppriate ssh config (ssh-config script will generate one).

robotsDir="../../Install/Robots"
allRobots=""

for robot in `ls ${robotsDir}`
do
  while read line; do
    echo $line | grep "BASE_WLAN0_IP=\""
    if [ $? -eq 0 ] ; then
      baseIP=$(echo $line | sed -n -e "s/BASE_WLAN0_IP=\"//" -e "s/\"// p")
    fi
    echo $line | grep "ROBOT_ID=\""
    if [ $? -eq 0 ] ; then
      robotID=$(echo $line | sed -n -e "s/ROBOT_ID=\"//" -e "s/\"// p")
    fi
  done < "$robotsDir/$robot/wireless"

  #check if robot is alive
  ping -c 1 -W 1 $baseIP$robotID &>/dev/null
  if [ $? -eq 0 ] ; then
    allRobots="$allRobots `echo ${robot} | tr '[:upper:]' '[:lower:]'`"
  fi
done

cssh ${allRobots}
